<?php

class SlidesTemplate{
	public static function generateSlideMetabox($post){
	// Add an nonce field so we can check for it later.
	wp_nonce_field( 'slide_meta_box', 'slide_meta_box_nonce' );

	/*
	 * Use get_post_meta() to retrieve an existing value
	 * from the database and use the value for the form.
	 */
	$slide_link = get_post_meta( $post->ID, '_slide_link', true );
	$slide_image = get_post_meta( $post->ID, '_slide_image', true);
	$thumbnail_id = get_post_meta ( $post->ID, '_thumbnail_id', true);
	$slide_text = get_post_meta ( $post->ID, '_slide_text', true);
	?>
	<p>
	    <label for="meta-textarea" class="prfx-row-title">Enlace  del slide</label>
	    <input type="text" name="slide_link" id="meta-textarea" value="<?php echo esc_attr( $slide_link );?>" size="60" />
	</p>
	<p>
	    <label for="meta-textarea" class="prfx-row-title">Texto  del slide</label>
	    <input type="text" name="slide_text" id="meta-textarea" value="<?php echo esc_attr( $slide_text );?>" size="60" />
	</p>
	<p>
	    <label for="meta-image" class="prfx-row-title">Imagen del slide</label>
	    <input type="text" name="slide_image" id="meta-image" size="60"  value="<?php if ( $slide_image!=="" ) { echo $slide_image; } ?>" />
	    <input type="button" id="meta-image-button" class="button" value="Escoge una Imagen" />
	    <input type="hidden" id="thumbnail-id" name="thumbnail_id" value="<?php echo esc_attr( $thumbnail_id);?>" />
	</p>
	<?php
	}

	public static function saveSlideMetabox($post_id){
		
		if ( ! isset( $_POST['slide_meta_box_nonce'] ) ) {
			return;
		}
		if ( ! wp_verify_nonce( $_POST['slide_meta_box_nonce'], 'slide_meta_box' ) ) {
			return;
		}

		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		//TODO: Should check capabilities
		/*if ( isset( $_POST['slide'] ) && 'page' == $_POST['post_type'] ) {
			if ( ! current_user_can( 'edit_slide', $post_id ) ) {
				return;
			}
		} */
		if ( ! isset( $_POST['slide_link'] ) ) {
			return;
		}
		$slide_link  = sanitize_text_field( $_POST['slide_link'] );
		$slide_text  = sanitize_text_field( $_POST['slide_text'] );
		$slide_image = sanitize_text_field( $_POST['slide_image'] );
		$thumbnail_id = sanitize_text_field( $_POST['thumbnail_id'] );
		update_post_meta( $post_id, '_slide_text', $slide_text );
		update_post_meta( $post_id, '_slide_link', $slide_link );
		update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id);
		update_post_meta( $post_id, '_slide_image', $slide_image );
	}
}
